package cn.netjava.robo3d.device;

import java.util.ArrayList;
import java.util.List;

import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;

/**
 * 坦克履带
 * 
 * @author Administrator
 * 
 */
public class CaterPilar extends Node {

	private float speed = 0;
	private List<Box> boxs;
	private Vector3f points[]; // 履带初始位置点
	private int nextindexpo[]; //  正速用 各个板子下个位置索引
    private int nextindexne[];
    private int nextindex[];
	public CaterPilar() {
		int count = 60;
		boxs = new ArrayList();
		points = new Vector3f[count];
		nextindex = new int[count];
		nextindexpo = new int[count];
		nextindexne = new int[count];
		for (int i = 0; i < count; i++) {
			Box box = new Box("box" + i, new Vector3f(-0.6f, -0.1f, -5),
					new Vector3f(0.6f, 0.1f, 5));
			box.setLocalScale(0.4f);
			if (i < count * 2 / 6) { // 第一段
				float step = 16 / (count * 2 / 6f);
				float x = (-8 + step * i);
				points[i] = new Vector3f(x, -3, 0);
			}
			if (count * 2 / 6 <= i && i < count * 3 / 6) {// 第二段
				float step = (FastMath.PI) / (count * 1 / 6);
				float angle = -FastMath.PI / 2 + step * (i - count * 2 / 6);
				points[i] = new Vector3f(3 * FastMath.cos(angle) + 8,
						3 * FastMath.sin(angle), 0);
			}
			if (count * 3 / 6 <= i && i < count * 5 / 6) {// 第三段
				float step = 16 / (count * 2 / 6f);
				points[i] = new Vector3f(8 - step * (i - count * 3 / 6), 3, 0);
			}
			if (count * 5 / 6 <= i && i < count) {// 第四段
				float step = (FastMath.PI) / (count * 1 / 6);
				float angle = FastMath.PI / 2 + step * (i - count * 5 / 6);
				points[i] = new Vector3f(3 * FastMath.cos(angle) - 8,
						3 * FastMath.sin(angle), 0);
			}
			box.setLocalTranslation(points[i]);

			for (int j = 0; j < boxs.size(); j++) {
				//正速度
					if (j == (boxs.size() - 1)) {
						nextindexpo[j] = 0;
					} else {
						nextindexpo[j] = j + 1;
					}
                //反速度
					if (j == 0) {
						nextindexne[j] = 59;
					} else {
						nextindexne[j] = j - 1;
					}
				}
			
			boxs.add(box);
			this.attachChild(box);
			this.setLocalScale(0.1f);
			Quaternion qua = new Quaternion();
			qua.fromAngleAxis(FastMath.PI / 2, new Vector3f(0, 1, 0));
			this.setLocalRotation(qua);
		}
	}

	private void setspeed(float speed) {
		this.speed = speed;

	}

	public void update(float time, float speed) {
		setspeed(speed);
		if(speed>0){
			nextindex=nextindexpo;
		}else{
		    nextindex=nextindexne;	
		}
		for (int i = 0; i < boxs.size(); i++) {
			Box current = boxs.get(i);
//			System.out.println("loc:"+current.getLocalTranslation());
			Vector3f direct;

			direct = points[nextindex[i]].subtract(
					current.getLocalTranslation()).normalize();
			if (speed < 0)
				direct.negateLocal();
			current.setLocalTranslation(current.getLocalTranslation().add(
					direct.mult(time * speed)));
			float distance = current.getLocalTranslation().distance(
					points[nextindex[i]]);
			if (distance < 0.3f) {// 和下一个点太近了,下一个点要变一位
				if (speed > 0) {
					if (nextindexpo[i] == (boxs.size() - 1)) {
						nextindexpo[i] = 0;
					} else {
						// System.out.println("lvdai!!!!!!!!");
						nextindexpo[i]++;
					}
				} else {
					if (nextindexne[i] == 0) {
						nextindexne[i] = 59;
					} else {
						nextindexne[i]--;
					}
				}
			}
		}
	}
}
